package utils;

import android.app.DownloadManager;
import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.lang.reflect.Method;
import java.util.Map;

import common.ISendData;

/**
 * Created by Admin on 2016/3/11.
 */
public class HttpRequestUtils {
    RequestQueue rQueue;
    public HttpRequestUtils(Context context){
      rQueue  = Volley.newRequestQueue(context);
    }

    //Get方法
    public  void getRequestString(String url){
        StringRequest req = new StringRequest(Request.Method.GET,url,
                //回调响应
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {

                    }
                    //出错回调
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }

        });
        rQueue.add(req);
    }

    public void postRequestString(String url,final Map<String,String> map,final Map<String,String> headers,final ISendData iSendData){
        StringRequest req = new StringRequest(Request.Method.POST,url,
                //回调响应
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        iSendData.sendData(s);
                    }
                    //出错回调
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            //输入参数
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                if(null != headers){
                    return headers;
                }
                return null;
            }
        };


        rQueue.add(req);
    }

}
